<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class template {

    var $template_data = array();
    //var $template_data_default = array();
    var $data_defaultx = array();
    var $mytheme;
    var $url_theme;
    var $url_page;
    var $url_admin;
    var $url_theme_admin;
    var $_ci;
    //Js load default
    var $arr_js = array();
    var $arr_script_content = array();
    var $tpl_prefix_admin = "admin/";
    var $prefix_frontend = "frontend/";
    var $arr_css = array();

    function __construct() {
        $this->_ci = &get_instance();
        $this->_ci->load->helper('url');

        $this->mytheme = isset($_SESSION['template']) ? $_SESSION['template'] : 'braygames';
        $this->url_theme = base_url() . 'template/braygames/';
        $this->url_theme_admin = base_url() . 'template/admin/';
        $this->url_page = base_url();
        $this->url_admin = base_url() . "admin/";


        $this->template_data = array(
            'layout_main' => 'template',
        );
    }

    function set($name, $value) {
        //$this->template_data[$name] = $value;
    }

    function set_var($name = '', $view = '', $view_data = array(), $user_template = '') {
        $this->set($name, $this->_ci->load->view($user_template . '/' . $view, $view_data, TRUE));
    }

    //load view
    /**
      $arr_theme = array(
      'layout_main'=> 'main', //File main in view
      //var => theme, exp:
      'var'=>array(
      'main_content_header' =>,
      'main_content_left' =>,
      'main_content_right'=>'',
      'main_content_footer'=>'',
      ));
      ver 2.0
     */
    function view($arr_theme = array(), $view_data = array(), $return = FALSE) {
        $arr_themex = array();
        $arr_theme = $this->template_data = array_merge($this->template_data, $arr_theme);
        $layout_main = $arr_theme['layout_main'];
		if(function_exists('module_instance')) {
			module_instance()->set_layout($layout_main);
		}
        unset($arr_theme['layout_main']);
        foreach ($arr_theme as $key => $val) {
            if ($val == "")
                $arr_themex[$key] = null;
            else
                $arr_themex[$key] = $this->_ci->load->view($val, $view_data, TRUE);
        }
        $this->_ci->load->view($layout_main, $arr_themex, $return);
    }
	
    function load_view($view = '', $view_data = array(), $return = FALSE) {
        return $this->_ci->load->view($this->mytheme . '/' . $view, $view_data, $return);
    }
 
    function add_css($filename, $patch = '') {
	if($patch =='')
		$patch = $this->url_theme;
        $css = '<link href="' . $patch . 'css/' . $filename . '" rel="stylesheet" type="text/css" />';
        return $css;
    }

    function add_js($filename, $patch = '') {
	if($patch =='')
		$patch = $this->url_theme;
       $js = '<script type="text/javascript" src="' .$patch . 'js/' . $filename . '"></script>';
        return $js;
    }

    function add_js_lib($filename = '', $localtion = '') {
        if (!in_array($filename, $this->arr_js))
            $this->arr_js[] = $filename;
    }

    function add_css_lib($filename = '', $localtion = '') {
        if (!in_array($filename, $this->arr_css))
            $this->arr_css[] = $filename;
    }

    function add_script_content($content = '') {
        $this->arr_script_content[] = $content;
    }

    function get_script_content() {
        for ($i = 0; $i < count($this->arr_script_content); $i++) {
            echo $this->arr_script_content[$i];
        }
    }

    function get_js_lib() {
        $txtjs = "";
        for ($i = 0; $i < count($this->arr_js); $i++) {
            if ($this->arr_js[$i] != "")
                echo $this->add_js($this->arr_js[$i]);
        }
        return $txtjs;
    }

    function get_css_lib() {
        $txtcss = "";
        for ($i = 0; $i < count($this->arr_css); $i++) {
            if ($this->arr_css[$i] != "")
                $txtcss.= $this->add_css($this->arr_css[$i]);
        }
        return $txtcss;
    }

    function redirect($url = '', $method = 'refresh', $code = null) {
        if ($url == '' || $url == 'url_page')
            $url = $this->url_page;
        return redirect((@$_SERVER['HTTP_REFERER'] && $url != $this->url_page) ? $_SERVER['HTTP_REFERER'] : $url, $method, $code);
    }
	
	/*
		view of admin page
	*/
	function viewAdmin($arr_theme = array(), $view_data = array(), $return = FALSE) {
        $arr_themex = array();
        foreach ($arr_theme as $key => $val) {
            if ($val == "")
                $arr_themex[$key] = null;
            else
                $arr_themex[$key] = $this->tpl_prefix_admin.$val;
        }
        return $this->view($arr_themex, $view_data, $return);
    }
	
	function add_admin_js_lib($filename = '', $localtion = '') {
        if (!in_array($filename, $this->arr_js))
            $this->arr_js[] = $filename;
    }

    function add_admin_css_lib($filename = '', $localtion = '') {
        if (!in_array($filename, $this->arr_css))
            $this->arr_css[] = $filename;
    }
	
	function get_admin_js_lib() {
        $txtjs = "";
        for ($i = 0; $i < count($this->arr_js); $i++) {
            if ($this->arr_js[$i] != "")
                echo $this->add_js($this->arr_js[$i],$this->url_theme_admin);
        }
        return $txtjs;
    }

    function get_admin_css_lib() {
        $txtcss = "";
        for ($i = 0; $i < count($this->arr_css); $i++) {
            if ($this->arr_css[$i] != "")
                $txtcss.= $this->add_css($this->arr_css[$i],$this->url_theme_admin);
        }
        return $txtcss;
    }
}

/* End of file Template.php */
/* Location: ./system/application/libraries/Template.php */